import requests
from langchain.tools import BaseTool
import yaml
from pydantic import BaseModel,Field
from typing import Type

# with open('./weather.yaml', 'r', encoding='utf-8') as f:
#     weather_tool = yaml.load(f, Loader=yaml.FullLoader)

api_key = "S4elbMFsDCL2c8REx"


def get_weather(location):
    url = rf'https://api.seniverse.com/v3/weather/now.json?key={api_key}&location={location}&language=zh-Hans&unit=c'
    response = requests.get(url)
    data = response.json()
    print("get_weather ==> ", data)
    return data

class WeatherParam(BaseModel):
    location: str = Field(..., description="城市名称, 例如： 西安")


class WeatherTool(BaseTool):
    name:str = "weather"
    description:str = "查询天气"
    args_schema: Type[BaseModel] = WeatherParam

    def _run(self, location: str) -> str:
        return get_weather(location)

